package cn.itcast.web.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;

import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cargo/export")
@Log4j
public class ExportController extends BaseController {
    @Reference
    private ContractService contractService;
    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;
    @RequestMapping("/contractList")
    public String contractList(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize){
        log.info("合同管理查询开始");
        //创建查询对象
        ContractExample contractExample = new ContractExample();
        //根据创建时间倒叙排列
        contractExample.setOrderByClause("create_time desc");
        ContractExample.Criteria criteria = contractExample.createCriteria();
        //根据企业id查询购销合同
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        //根据合同状态查询所有状态为1 的合同
        criteria.andStateEqualTo(1);

        //分页
        PageInfo<Contract> pageInfo =
                contractService.findByPage(contractExample,pageNum,pageSize);

        //报存
        request.setAttribute("pageInfo",pageInfo);


        return  "cargo/export/export-contractList";
    }

    //进入报运页面
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        log.info("报运单查询开启");
      //创建查询对象
        ExportExample example = new ExportExample();
        //创建查询条件  // 根据创建时间倒序排序..
        example.setOrderByClause("create_time desc");

        ExportExample.Criteria criteria = example.createCriteria();
        //根据企业信息查询所有购销合同
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        //分页查询

        PageInfo<Export> pageInfo = exportService.findByPage(example, pageNum, pageSize);

        //保存
        request.setAttribute("pageInfo",pageInfo);

        // 合同管理列表页面，显示状态为1的购销合同
        return "cargo/export/export-list";
    }

    //添加
    @RequestMapping("/toExport")
    public String toExport(String id){
        /**
         * 报运单，进入添加页面
         * 请求参数：
         *      id  多个购销合同的id，自动以逗号隔开
         */
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }

    //保存   保单的提交或修改
    @RequestMapping("/edit")
    public String edit(Export export){
        //设置企业信息
        export.setCompanyId(getLoginCompanyId());
        export.setCompanyName(getLoginCompanyName());
        //判断是添加还是更新
        if(StringUtils.isEmpty(export.getId())){
            //保存数据
            export.setCreateBy(getLoginUser().getId());
            export.setCreateDept(getLoginUser().getDeptId());
            export.setCreateTime(new Date());

            exportService.save(export);
        }
        else {
            exportService.update(export);
        }
        return  "redirect:/cargo/export/list";

    }

    /**
     * 报运单修改，进入修改页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        // 1. 根据报运单的id查询报运单
        Export export = exportService.findById(id);

        // 2. 根据报运单的id查询报运的商品
        ExportProductExample example = new ExportProductExample();
        example.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> eps = exportProductService.findAll(example);

        // 3. 保存
        request.setAttribute("export",export);
        request.setAttribute("eps",eps);

        // 4. 跳转到修改页面
        return "cargo/export/export-update";
    }

    @RequestMapping("toView")
    public String view(){
        return "cargo/export/export-view";
    }

    @RequestMapping("/exportE")
    public String exportE(String id){

        return "redirect:/cargo/export/list";
    }

    /**
     * 报运单的提交、取消
     */
    @RequestMapping("/submit")
    public String submit(String id) {
        Export export = new Export();
        export.setId(id);
        // 修改状态：已提交的购销合同才可以电子报运
        export.setState(1);
        exportService.update(export);
        return "redirect:/cargo/export/list";
    }
    @RequestMapping("/cancel")
    public String cancel(String id) {
        Export export = new Export();
        export.setId(id);
        // 修改状态：设置状态为0草稿
        export.setState(0);
        exportService.update(export);
        return "redirect:/cargo/export/list";
    }
}
